package com.citybox.infrastructure.activity.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.citybox.domain.activity.ActivityInfoDo;
import com.citybox.infrastructure.activity.dataobject.ActivityInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zhao.x
 */
@Mapper
public interface ActivityInfoMapper extends BaseMapper<ActivityInfo> {

    @Select({"<script>",
            "SELECT ",
            "tai.activity_title, ",
            "tai.activity_picture_id, ",
            "tai.activity_describe, ",
            "tai.finish_time, ",
            "ati.activity_name AS activity_type_name, ",
            "CASE ",
            "   WHEN ROUND(ST_Distance_Sphere(point(#{query.longitude}, #{query.latitude}), point(tai.longitude, tai.latitude))) &lt; 1000 ",
            "   THEN CONCAT(ROUND(ST_Distance_Sphere(point(#{query.longitude}, #{query.latitude}), point(tai.longitude, tai.latitude))), '米') ",
            "   ELSE CONCAT(ROUND(ST_Distance_Sphere(point(#{query.longitude}, #{query.latitude}), point(tai.longitude, tai.latitude)) / 1000, 1), '公里') ",
            "END AS distance_display, ",
            "ROUND(ST_Distance_Sphere(point(#{query.longitude}, #{query.latitude}), point(tai.longitude, tai.latitude))) AS distance_meters ",
            "FROM t_activity_info tai ",
            "LEFT JOIN t_activity_type_info ati ON tai.activity_type_id = ati.activity_type ",
            "WHERE tai.address_code LIKE CONCAT('%', #{query.addressCode}, '%')",
            "<if test='query.activityTitle != null and query.activityTitle != \"\"'>",
            "   AND tai.activity_title LIKE CONCAT('%', #{query.activityTitle}, '%')",
            "</if>",
            "<if test='query.activityTypeId != null and query.activityTypeId != \"\"'>",
            "   AND tai.activity_type_id LIKE CONCAT('%', #{query.activityTypeId}, '%')",
            "</if>",
            "ORDER BY distance_meters ASC, tai.create_time ASC",
            "</script>"})
    List<ActivityInfoDo> pageList(@Param("query") ActivityInfoDo query);
}
